/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import entity.Product;
import javax.ejb.EJB;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import service.ProductServiceLocal;
import service.SaleOrderDetailServiceLocal;

/**
 *
 * @author SONY
 */
public class DeleteProductStrutsAction extends org.apache.struts.action.Action {
    
    @EJB ProductServiceLocal productServiceLocal;
    @EJB SaleOrderDetailServiceLocal saleOrderDetailServiceLocal;

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        try {
            if(request.getQueryString() != null) {
                ServletContext context = servlet.getServletContext();
                productServiceLocal = (ProductServiceLocal) context.getAttribute("productServiceLocal");
                saleOrderDetailServiceLocal = (SaleOrderDetailServiceLocal) context.getAttribute("saleOrderDetailServiceLocal");
                String pid = request.getQueryString().toString();
                Product p_find = productServiceLocal.getProductDetails_byPID(pid);
                if(p_find != null) {
                    Product p = saleOrderDetailServiceLocal.exist_Product(pid);
                    if(p == null) {
                        if(productServiceLocal.deleteProduct(pid)) {
                            request.setAttribute("notify", "Xóa Sản Phẩm Thành Công!");
                            return mapping.findForward(SUCCESS);
                        }  
                    } else {
                        request.setAttribute("notify", "Sản Phẩm Này Không Được Xóa Vì Dữ Liệu Có Liên Quan Đến Bảng Khác.");
                        return mapping.findForward(SUCCESS);
                    }
                } else {
                    request.setAttribute("notify", "Mã Sản Phẩm này không tồn tại! Hãy kiểm tra lại.");
                    return mapping.findForward(SUCCESS);                    
                }
            }
        } catch(Exception e) {
            System.err.println(e);
        }
        request.setAttribute("notify", "Xóa Sản Phẩm Thất Bại Do Bị Gián Đoạn. Hãy Thực Hiện Lại Sau!");
        return mapping.findForward(SUCCESS);
    }
}
